﻿// Type: VSFile.VisualStudioFile
// Assembly: VSFile, Version=1.2.1.0, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\Downloads\VSFile.dll

using System;
using System.IO;

namespace ReadSolutionContents
{
    /// <summary>
    /// Represents a Visual Studio file.
    /// 
    /// </summary>
    public abstract class VisualStudioFile
    {
        /// <summary>
        /// File extension of Visual Studio file.
        /// 
        /// </summary>
        private string m_fileExtension;
        /// <summary>
        /// File path to Visual Studio file.
        /// 
        /// </summary>
        private string m_filePath;

        /// <summary>
        /// Get path to directory containing file.
        /// 
        /// </summary>
        /// 
        /// <value>
        /// String representing path to directory containing this Visual Studio file.
        /// 
        /// </value>
        public string DirectoryPath
        {
            get
            {
                return Path.GetDirectoryName(this.FilePath);
            }
        }

        /// <summary>
        /// Get file extension.
        /// 
        /// </summary>
        /// 
        /// <value>
        /// String representing file extension of this Visual Studio file.
        /// 
        /// </value>
        public string FileExtension
        {
            get
            {
                return this.m_fileExtension;
            }
        }

        /// <summary>
        /// Get file name.
        /// 
        /// </summary>
        /// 
        /// <value>
        /// String representing file name of this Visual Studio file.
        /// 
        /// </value>
        public string FileName
        {
            get
            {
                return Path.GetFileName(this.FilePath);
            }
        }

        /// <summary>
        /// Get file name with no file extension.
        /// 
        /// </summary>
        /// 
        /// <value>
        /// String representing file name of this Visual Studio file with no file extension.
        /// 
        /// </value>
        public string FileNameNoExtension
        {
            get
            {
                return Path.GetFileNameWithoutExtension(this.FilePath);
            }
        }

        /// <summary>
        /// Get file path.
        /// 
        /// </summary>
        /// 
        /// <value>
        /// String representing file path to this Visual Studio file.
        /// 
        /// </value>
        public string FilePath
        {
            get
            {
                return this.m_filePath;
            }
        }

        /// <summary>
        /// Constructor.
        /// 
        /// </summary>
        /// <param name="fileExtension">String representing file extension of Visual Studio file.
        ///             </param><param name="filePath">String representing file path to Visual Studio file.
        ///             </param>
        protected VisualStudioFile(string fileExtension, string filePath)
        {
            if (string.IsNullOrEmpty(fileExtension) || string.IsNullOrEmpty(filePath))
                throw new ArgumentException();
            this.m_fileExtension = fileExtension;
            this.m_filePath = filePath;
        }

        /// <summary>
        /// Load file.
        /// 
        /// </summary>
        public void Load()
        {
            this.CheckFilePath();
            this.CheckFileExtension();
            this.ReadFile();
        }

        /// <summary>
        /// Get full path combining directory path and given relative path.
        /// 
        /// </summary>
        /// <param name="relativePath">String representing relative path.
        ///             </param>
        /// <returns>
        /// String representing full path combining directory path and given relative path.
        /// 
        /// </returns>
        protected string GetFullPath(string relativePath)
        {
            return Path.Combine(this.DirectoryPath, relativePath);
        }

        /// <summary>
        /// Read file.
        /// 
        /// </summary>
        protected abstract void ReadFile();

        /// <summary>
        /// Throw exception if file extension is invalid.
        /// 
        /// </summary>
        private void CheckFileExtension()
        {
            if (!Path.GetExtension(this.FilePath).Equals(this.FileExtension, StringComparison.OrdinalIgnoreCase))
                throw new IOException("Invalid file extension.");
        }

        /// <summary>
        /// Throw exception if file path is invalid.
        /// 
        /// </summary>
        private void CheckFilePath()
        {
            if (!File.Exists(this.FilePath))
                throw new FileNotFoundException("File not found at path: " + this.FilePath);
        }
    }
}
